﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2017/11/8 13:40:05
*说明：<Function>
/**********************************************************************/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    /// <summary>
    /// Json字符串解析
    /// </summary>
    public static class JsonHelper
    {
        /// <summary>
        /// 解析JSON字符串生成对象实体
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
        /// <returns>对象实体</returns>
        public static T DeserializeJsonToObject<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T t = o as T;
            return t;
        }
        /// <summary>
        /// 将对象序列化为JSON格式
        /// </summary>
        /// <param name="o">对象</param>
        /// <returns>json字符串</returns>
        public static string SerializeObject(object o)
        {
            string json = JsonConvert.SerializeObject(o);
            return json;
        }
        /// <summary>
        /// 保存对象到文件
        /// </summary>
        /// <param name="o"></param>
        /// <param name="filename"></param>
        public static void SaveToFile(object o,string filename)
        {
            string str = SerializeObject(o);
            string dir = Path.GetDirectoryName(filename);
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
            StreamWriter sw = File.CreateText(filename);
            sw.WriteLine(str);
            sw.Close();
        }
        /// <summary>
        /// 读取文件到对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static T ReadFromFile<T>(string filename) where T : class
        {
            if (File.Exists(filename) == false) return null;
            StreamReader mysr = new StreamReader(filename, Encoding.UTF8);
            string json = mysr.ReadToEnd();
            mysr.Close();
            
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T t = o as T;
            return t;

        }
        public static string GetByJObject(string json, string itemname)
        {
            JObject jobj = JObject.Parse(json);
            return jobj[itemname].ToString();
        }
       
        public static List<string> GetByJarray(string json, string itemname)
        {
            List<string> lst = new List<string>();
            JArray jarr = JArray.Parse(json);
            foreach (var item in jarr)
            {
                try
                {
                    lst.Add(item[itemname].ToString());
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return lst;
                }
            }
            return lst;
        }
    }
}
